/* 变量 */
var guaguacard = "other/guaguacard.html";


// 刮刮卡
var contactsCallback_guaguacard = myApp.onPageInit('guaguacard', function (page) {

    // if (!hasAccount() || !hasOpenId()) {
    //     $$("#guaguacard_page .noguaguacard").show();
    //     return;
    // }

    //处理参数
    var account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
    var openid = iss_localStorage.get("yuqing_appOpenId"); // 全局变量
    // var account = iss_localStorage.get("yuqing_appAccount") === ''?'b760689cb63a4a92834d6f14173bc293' : iss_localStorage.get("yuqing_appAccount"); // 全局变量
//    var openid = iss_localStorage.get("yuqing_appOpenId") === ''? 'oX816v4uhBMEXChYlScNbcq-uf0Y' : iss_localStorage.get("yuqing_appOpenId"); // 全局变量

    var zjl = false;
    var num = 0;
    var goon = true;
    var recordGuid = ""; // 记录编号
    var prize = ""; // 奖项名称
    var errorMsg = ""; // 错误提示

    // 刮奖效果
    function loading(canvas, options) {
        this.canvas = canvas;
        if (options) {
            this.radius = options.radius || 12;
            this.circleLineWidth = options.circleLineWidth || 4;
            this.circleColor = options.circleColor || 'lightgray';
            this.moveArcColor = options.moveArcColor || 'gray';
        } else {
            this.radius = 12;
            this.circelLineWidth = 4;
            this.circleColor = 'lightgray';
            this.moveArcColor = 'gray';
        }
    }
    loading.prototype = {
        show: function () {
            var canvas = this.canvas;
            if (!canvas.getContext) return;
            if (canvas.__loading) return;
            canvas.__loading = this;
            var ctx = canvas.getContext('2d');
            var radius = this.radius;
            var me = this;
            var rotatorAngle = Math.PI * 1.5;
            var step = Math.PI / 6;
            canvas.loadingInterval = setInterval(function () {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                var lineWidth = me.circleLineWidth;
                var center = {
                    x: canvas.width / 2,
                    y: canvas.height / 2
                };

                ctx.beginPath();
                ctx.lineWidth = lineWidth;
                ctx.strokeStyle = me.circleColor;
                ctx.arc(center.x, center.y + 20, radius, 0, Math.PI * 2);
                ctx.closePath();
                ctx.stroke();
                //在圆圈上面画小圆
                ctx.beginPath();
                ctx.strokeStyle = me.moveArcColor;
                ctx.arc(center.x, center.y + 20, radius, rotatorAngle, rotatorAngle + Math.PI * .45);
                ctx.stroke();
                rotatorAngle += step;

            },
				100);
        },
        hide: function () {
            var canvas = this.canvas;
            canvas.__loading = false;
            if (canvas.loadingInterval) {
                window.clearInterval(canvas.loadingInterval);
            }
            var ctx = canvas.getContext('2d');
            if (ctx) ctx.clearRect(0, 0, canvas.width, canvas.height);
        }
    };

    var useragent = window.navigator.userAgent.toLowerCase();
    // 刮奖
    $("#scratchpad").wScratchPad({
        width: 150,
        height: 40,
        color: "#a9a9a7",
        scratchMove: function () {
            if (!hasUserIdNothing()) {
                return;
            }
            num++;
            /* if (zjl && num > 30 && goon) { */
            if (num > 30 && goon) {
                goon = false;

                SaveData();
            }

            if (useragent.indexOf("android 4") > 0) {
                if ($("#scratchpad").css("color").indexOf("51") > 0) {
                    $("#scratchpad").css("color", "rgb(50,50,50)");
                } else if ($("#scratchpad").css("color").indexOf("50") > 0) {
                    $("#scratchpad").css("color", "rgb(51,51,51)");
                }
            }

        },
        scratchDown: function () {
            CheckPopLogin(function () {
                account = iss_localStorage.get("yuqing_appAccount"); // 全局变量
                openid = iss_localStorage.get("yuqing_appOpenId"); // 全局变量
                LoadData();
            });
        }
    });

    // 初始化数据
    LoadData();

    // 加载数据
    function LoadData() {
        $$.ajax({
            url: ISS_Constants.Url.const_weixin_guaguacard,
            data: { "action": "1", "conditionParm": "{'AccountGuid':'" + account + "','OpenId':'" + openid + "'}" },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if (data && data.success && data.datas) {
                    var datas = data.datas;

                    // if (prize != null || datas.Activity != null || datas.ActivityAwards != null) {
                    //     $$("#guaguacard_page .guaguacardcontent").show();
                    // }
                    prize = datas.Name;
                    if (prize != null) {
                        $("#prize").html(datas.Name);
                    }


                    if (datas.IsAwards) {
                        zjl = true;
                    }

                    // 活动说明
                    var Content = "";
                    if (datas.Activity != null) {
                        Content = "<p class='red'>本次活动总共可以刮" + datas.Activity.ActivityPersonMaxTimes + "次,当天可以刮" + datas.Activity.ActivityDayMaxTime + "次,你已经刮了" + datas.Times + "次,机会如果没用完重新进入本页面可以再刮!</p>"
                        $("#guaguacard_page #Content").html(Content);
                    }
                    // 奖品信息
                    var Awards = "";
                    if (datas.ActivityAwards != null) {
                        for (var i = 0; i < datas.ActivityAwards.length; i++) {
                            var p = "<p>" + datas.ActivityAwards[i].AwardsTitle + "： " + datas.ActivityAwards[i].AwardsName + "。奖品数量：" + datas.ActivityAwards[i].AwardsNum + " </p>";
                            Awards += p;
                        }
                        $("#guaguacard_page #Awards").html(Awards);
                    }
                    // 记录
                    if (datas.ActivityRecords && datas.ActivityRecords.length > 0
                        && datas.ActivityRecords[0] && datas.ActivityRecords[0] != null) {
                        recordGuid = datas.ActivityRecords[0].RecordGUID;
                    }
                    // 查询中奖纪录
                    SelectWinningRecord(datas.Activity.ActivityGUID);

                    errorMsg = data.msg;
                    $$("#guaguacard_page .guaguacardcontent").show();


                } else { /* alert("没有活动信息"); */$$("#guaguacard_page .noguaguacard").show(); }
            },
            error: function (xhr, status) {
                $$("#guaguacard_page .noguaguacard").show();
                console.log("guaguacard ajax error!");
            }
        }); // end ajax
    }; // end loading

    // 保存数据
    function SaveData() {
        if (recordGuid == undefined || recordGuid == null || recordGuid == "") {
            myApp.alert(errorMsg);
            return;
        }
        $$.ajax({
            url: ISS_Constants.Url.const_weixin_guaguacard,
            data: { "action": "2", "conditionParm": "{'RecordGuid':'" + recordGuid + "'}" },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if (data && data.success) {
                    if (zjl) {
                        myApp.alert('恭喜您获得 ' + $("#guaguacard_page #prize").html());
                    } else {
                        myApp.alert($("#guaguacard_page #prize").html());
                    }
                }
            },
            error: function (xhr, status) {
                console.log("ajax error!");
            }
        }); // end ajax
    }; // end save

    // 查询中奖纪录
    function SelectWinningRecord(activityGuid) {
        $$.ajax({
            url: ISS_Constants.Url.const_weixin_guaguacard,
            data: { "action": "7", "conditionParm": "{'ActivityGuid':'" + activityGuid + "'}" },
            crossDomain: true,
            dataType: "json",
            success: function (data, status) {
                if (data && data.success && data.datas) {
                    var datas = data.datas;
                    var ul = "";
                    if (datas.length > 0) {
                        for (var i = 0; i < datas.length; i++) {

                            var li = "<li class='item-content'>";
                            li += "<div class='item-inner'>";
                            li += "<div class='item-title'>" + datas[i].AwardsTitle + "</div>";
                            li += "<div class='item-after'>" + datas[i].NickName + "</div>";
                            li += " </div></li>";

                            ul += li;
                        }
                        $$('#guaguacard_page #ul_guaguacard').html(ul);
                    }
                }
            },
            error: function (xhr, status) {
                console.log("ajax error!");
            }
        }); // end ajax
    }; // end fun
});


function initData() {
    var href = window.location.href;
    if (href.indexOf(guaguacard) > -1) {
        contactsCallback_guaguacard.trigger();
    }
}

initData();




